//**********************************加载文件配置****************************************
/**
 * @param VERSION 版本号
 * @param pathName 项目地址路径
 * @param company 项目简称
 * @param sitePath 域名路径
 * @param bootConfig 资源路径
 */
var version = version();
var pathName = pathName();
var company = company(pathName);
var sitePath = sitePath(pathName, company);
var bootConfig = {
    "default": {
        "config": [
            {
                "path": "../../css/mui/mui.min.css",
                "type": "css"
            },
            {
                "path": "../../css/mui/mui.picker.min.css",
                "type": "css"
            },
            {
                "path": "../../css/base.css",
                "type": "css"
            },
            {
                "path": "../../css/style.css",
                "type": "css"
            },
            {
                "path": "../../css/mui/mui.loading.css",
                "type": "css",
            },
            {
                "path": "../../js/jq/jQuery-2.2.0.min.js",
                "type": "javascript"
            },
            {
                "path": "../../js/base/webApi/webApi.js",
                "type": "javascript"
            },
            {
                "path": "../../js/common.js",
                "type": "javascript"
            },
            {
                "path": "../../js/mui/mui.js",
                "type": "javascript"
            },
            {
                "path": "../../js/mui/mui.picker.min.js",
                "type": "javascript"
            },
            {
                "path": "../../js/style.js",
                "type": "javascript"
            }
        ],
        "notes": "默认加载"
    },
    "appTable": {
        "config": [
            {
                "path": "../../js/table/dataTableFormat.js",
                "type": "javascript"
            },
            {
                "path": "../../js/table/appTable.js",
                "type": "javascript"
            }
        ],
        "notes": "列表默认加载"
    },
    "form": {
        "config": [
            {
                "path": "../../js/base/form/Form.js",
                "type": "javascript"
            },
            {
                "path": "../../js/flow/flow.js",
                "type": "javascript"
            }
        ],
        "notes": "表单默认加载"
    },
    "record": {
        "config": [
            {
                "path": "../../js/voice/recorder.js",
                "type": "javascript"
            }
        ],
        "notes": "下拉加载列表"
    },
    "appTabs": {
        "config": [
            {
                "path": "../../js/tabs/tabs.js",
                "type": "javascript"
            }
        ],
        "notes": "下拉加载列表"
    },
    "autoComplete": {
        "config": [
            {
                "path": "../../js/autocomplete/jquery.autocomplete.js",
                "type": "javascript"
            }
        ],
        "notes": "下拉加载列表"
    },
    "fileUpload": {
        "config": [
            {
                "path": "../../js/ajaxFileUpLoad/ajaxfileupload.js",
                "type": "javascript"
            },
        ],
        "notes": "下拉加载列表"
    },
    "picturePreview": {
        "config": [
            {
                "path": "../../js/mui/mui.zoom.js",
                "type": "javascript"
            },
            {
                "path": "../../js/mui/mui.previewimage.js",
                "type": "javascript"
            },
        ],
        "notes": "图片预览"
    },
    "tree": {
        "config": [
            {
                "path": "/css/ztree/treeStyle.css",
                "type": "css"
            },
            {
                "path": "/js/jq/ztree/jquery.ztree.min.js",
                "type": "javascript"
            },
            {
                "path": "/js/jq/ztree/fuzzysearch.js",
                "type": "javascript"
            },
            {
                "path": "/js/table/dataTableFormat.js",
                "type": "javascript"
            },
            {
                "path": "/js/jq/ztree/treeView.js",
                "type": "javascript"
            },
        ],
        "notes": "树默认"
    },
    "picker": {
        "config": [
            {
                "path": "../../css/mui/mui.poppicker.css",
                "type": "css"
            },
            {
                "path": "../../js/mui/mui.poppicker.js",
                "type": "javascript"
            },
        ],
        "notes": "下拉选择器"
    }
}


//************************************勿动******************************************
function version() {
    var ver = localStorage.getItem("_lastUpdateTime");
    if (ver == "" || ver == undefined || ver == null) {
        var now = new Date();
        ver = now.toISOString();
    }
    return ver;
}

function pathName() {
    return location.pathname;
}

function company(pathName) {
    var list = pathName.split('/');
    if (list == null || list.length <= 0) {
        return "";
    }
    return list[1].toLocaleLowerCase() == "html" ? "" : '/' + company;
}

function sitePath(pathName, company) {
    var hostName = location.hostname;
    var port = location.port;
    var protocol = location.protocol

    if (port == '' || port == '80') {
        port = '';
    } else {
        port = ':' + port;
    }
    return protocol + '//' + hostName + port + company;
}

function boot() {
    this.__GetJSPath = function (js) {
        var scripts = document.getElementsByTagName("script");
        var path = "";
        for (var i = 0, l = scripts.length; i < l; i++) {
            var src = scripts[i].src;
            if (src.indexOf(js) != -1) {
                var ss = src.split(js);
                path = ss[0];
                break;
            }
        }
        var href = location.href;
        href = href.split("#")[0];
        href = href.split("?")[0];
        var ss = href.split("/");
        ss.length = ss.length - 1;
        href = ss.join("/");
        if (path.indexOf("https:") == -1 && path.indexOf("http:") == -1
            && path.indexOf("file:") == -1 && path.indexOf("\/") != 0) {
            path = href + "/" + path;
        }
        return path;
    }, this.__getConfig = function (id) {
        var config = null;
        if (bootConfig) {
            return bootConfig[id];
        }
    }, this.__getParmID = function () {
        var id;
        var src = document.scripts[document.scripts.length - 1].src;
        var args = src.substr(src.indexOf("?") + 1).split("&");
        for (var i = 0; i < args.length; i++) {
            var j = args[i].indexOf("=");
            if (j > -1 && args[i].substr(0, j) == 'id') {
                id = args[i].substr(j + 1);

            }
        }
        return id;
    }, this.__load = function () {
        var id = this.__getParmID();
        this.__write(this.__getConfig("default"));
        if (id) {
            var list = id.split(",");
            for (var i = 0; i < list.length; i++) {
                this.__write(this.__getConfig(list[i]));
            }

        }
    }
        , this.__write = function (config) {
        if (config) {
            var temp = null;
            var bootPATH = this.__GetJSPath("boot.js");
            config = config.config;
            bootPATH = "";

            for (var i = 0; i < config.length; i++) {
                temp = config[i];
                if (temp.type != "link") {
                    if (temp.path.indexOf("?") > 0) {
                        temp.path = temp.path + "&ver=" + version;
                    } else {
                        temp.path = temp.path + "?ver=" + version;
                    }
                }
                if (temp) {
                    if (temp.type == "css") {
                        document.write('<link href="' + bootPATH + temp.path + '" rel="stylesheet" type="text/css" />');
                    } else if (temp.type == "javascript") {
                        document.write('<script src="' + bootPATH + temp.path + '" type="text/javascript"></script>');
                    } else {
                        this.__write(this.__getConfig(temp.path));
                    }
                }
            }
        }
    }
        , this.__loadNeed = function (id) {
        if (id) {
            var list = id.split(",");
            for (var i = 0; i < list.length; i++) {
                this.__writeNeed(this.__getConfig(list[i]));
            }

        }
    },
        this.__writeNeed = function (config) {
            if (config) {
                var temp = null;
                var bootPATH = this.__GetJSPath("boot.js");
                config = config.config;
                bootPATH = "";
                for (var i = 0; i < config.length; i++) {
                    temp = config[i];
                    if (temp) {
                        if (temp.type == "css") {
                            this.__createNeedJsOrCss(bootPATH + temp.path, "css")
                        } else if (temp.type == "javascript") {
                            this.__createNeedJsOrCss(bootPATH + temp.path, "script")
                        } else {
                            this.__writeNeed(this.__getConfig(temp.path));
                        }
                    }
                }
            }
        },
        this.__createNeedJsOrCss = function (url, type) {
            var xmlHttp = null;
            if (window.ActiveXObject) {
                try {
                    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
            } else if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();
            }
            if (url.indexOf("?") > 0) {
                url = url + "&ver=" + version;
            } else {
                url = url + "?ver=" + version;
            }
            xmlHttp.open("GET", url, false);
            xmlHttp.send(null);
            if (xmlHttp.readyState == 4) {
                if ((xmlHttp.status >= 200 && xmlHttp.status < 300) || xmlHttp.status == 0 || xmlHttp.status == 304) {
                    var myBody = document.getElementsByTagName("BODY")[0];
                    var myneed;
                    if ("script" == type) {
                        myneed = document.createElement("script");
                        myneed.language = "javascript";
                        myneed.type = "text/javascript";
                    }
                    if ("css" == type) {
                        myneed = document.createElement('style');
                        myneed.type = "text/css";
                        myneed.href = url;
                    }

                    try {
                        myneed.appendChild(document.createTextNode(xmlHttp.responseText));
                    } catch (ex) {
                        myneed.text = xmlHttp.responseText;
                    }
                    myBody.appendChild(myneed);
                }
            }
        }
}

var boot = new boot();
boot.__load();
